<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="用户名" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findHead()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建用户</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" width="100" label="序号"></el-table-column>
                <el-table-column prop="username" label="用户名" width="100" align="center"></el-table-column>
                <el-table-column label="性别" align="center" width="100">
                    <template slot-scope="scope">
                        <span>{{ scope.row.gender == '0' ? '男' : '女'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="birthdays" label="出生日期" align="center" width="150px"></el-table-column>
                <el-table-column prop="telephone" label="电话号码" align="center" width="150px"></el-table-column>

                <el-table-column label="禁用/启用" align="center" width="80px">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.station"
                                   active-color="#13ce66"
                                   inactive-color="#ff4949"
                                   :active-value="0"
                                   :inactive-value="1"
                                   @change=changSwitch(scope.row.station,scope.row.id)>
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注" align="center" width="220px"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="ChangePassword(scope.row)">修改密码</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                        <el-button type="primary" size="mini" @click="AddingRoles(scope.row)">添加角色</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="用户名" prop="username">
                                    <el-input v-model="formData.username"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="密码" prop="password">
                                    <el-input v-model="formData.password"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="注册日期">
                                    <el-date-picker v-model="formData.birthday" type="date" placeholder="选择日期">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12">
                                <el-form-item label="性别">
                                    <el-select v-model="formData.gender">
                                        <el-option label="男" value="0"></el-option>
                                        <el-option label="女" value="1"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="号码">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="状态">
                                    <el-select v-model="formData.station">
                                        <el-option label="开启" value="0"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="备注">
                                    <el-input v-model="formData.remark" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!--编辑窗口-->
            <div class="add-form">
                <el-dialog title="编辑用户" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="用户名" prop="username">
                                    <el-input v-model="formData.username"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="注册日期">
                                    <el-date-picker v-model="formData.birthday" type="date" placeholder="选择日期">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>

                            <el-col :span="12">
                                <el-form-item label="性别">
                                    <el-select v-model="formData.gender">
                                        <el-option label="男" value="0"></el-option>
                                        <el-option label="女" value="1"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="号码">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="状态">
                                    <el-select v-model="formData.station">
                                        <el-option label="开启" value="0"></el-option>
                                        <el-option label="关闭" value="1"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="备注">
                                    <el-input v-model="formData.remark" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!--修改密码-->
            <div class="add-form">
                <el-dialog title="修改密码" :visible.sync="updateByPassword">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-form-item label="密码" prop="cipher">
                            <el-input type="password" placeholder="请输入密码" v-model="formData.cipher"
                                      autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" prop="cryptogram">
                            <el-input type="password" placeholder="请输入密码" v-model="formData.cryptogram"
                                      autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="submitForm(fromData)">提交</el-button>
                            <el-button @click="resetForm('formData')">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>
            </div>

            <!--角色权限管理-->
            <div class="add-form">
                <el-dialog title="角色授权" :visible.sync="dialogFormVisibleUser">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <div class="checkScrol">
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>选择</th>
                                        <th>角色名称</th>
                                        <th>角色职务</th>
                                        <th>角色说明</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="c in tableData">
                                        <td width="100">
                                            <input :id="c.id" v-model="RoleIds" type="checkbox" :value="c.id">
                                        </td>
                                        <td><label :for="c.id">{{c.name}}</label></td>
                                        <td><label :for="c.id">{{c.keyword}}</label></td>
                                        <td><label :for="c.id">{{c.description}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleUser = false">取消</el-button>
                        <el-button type="primary" @click="RoleAuthorization()">确定</el-button>
                    </div>
                </el-dialog>
            </div>


        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="../plugins/healthmobile.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            activeName: 'first',//添加/编辑窗口Tab标签名称
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            RoleIds: [],//添加表单窗口中检查组复选框对应id
            tableData: [],//添加表单窗口中检查组列表数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            updateByPassword: false, //编辑密码是否可见
            dialogFormVisibleUser: false, //角色授权管理
            rules: {//校验规则
                username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                password: [{required: true, message: '请输入密码', trigger: 'blur'}],
                cipher: [{required: true, message: '请输入修改密码', trigger: 'blur'}],
                cryptogram: [{required: true, message: '请再次输入密码', trigger: 'blur'}]
            }

        },
        //钩子函数查询所有
        created() {
            this.findPage();
        },
        methods: {
            //分页查询
            findPage() {
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                //发送请求
                axios.post("/user/findPage.do", param).then((res) => {
                    //双向绑定
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            //从第一页查询
            findHead() {
                this.pagination.currentPage = 1;
                this.findPage();
            },
            //修改状态
            changSwitch(station, id) {
                //get传2个以上参数使用{params:{"station":station,"username":username}}  后台用 @RequestParam Map map 接收
                //post传2个以上参数使用{"station":station,"username":username}  后台用 @RequestBody Map map 接收
                axios.get("/user/station.do", {params: {"station": station, "id": id}}).then((res) => {
                    if (res.data.flag) {
                        this.$message({
                            message:res.data.message,
                            type:"success"
                        });
                    }else {
                        this.$message(res.data.message)
                    }
                })
            },
            //重置表单
            resetForm() {
                this.formData = {};
            },
            //新增表单窗口开启
            handleCreate() {
                this.dialogFormVisible = true;
                this.formData = {};
            },
            //添加用户
            handleAdd() {
                //验证手机号码是否正确
                var telephone = this.formData.telephone;
                //alert("sss")
                //校验手机号输入是否正确
                if (!checkTelephone(telephone)) {
                    this.$message.error('请输入正确的手机号');
                    //alert("bbb")
                    return;
                };
                axios.post("/user/add.do", this.formData).then((res) => {
                    this.dialogFormVisible = false;
                    if (res.data.flag) {
                        this.$message({
                            message: res.data.message,
                            type: "success"
                        })
                    } else {
                        this.$message.error(res.data.message)
                    }
                }).finally(() => {
                    this.findHead();
                });
            },
            //编辑回显
            handleUpdate(row) {
                this.dialogFormVisible4Edit = true;
                this.formData = {};
                axios.get("/user/findById.do?id=" + row.id).then((res) => {
                    if (res.data.flag) {
                        this.formData = res.data.data;
                    } else {
                        this.$message.error("页面获取失败请稍后再试！");
                    }
                })
            },
            //编辑确定
            handleEdit() {
                this.dialogFormVisible4Edit = false;
                //验证手机号码是否正确
                var telephone = this.formData.telephone;
                //校验手机号输入是否正确
                if (!checkTelephone(telephone)) {
                    this.$message.error('请输入正确的手机号');
                    return;
                }
                axios.post("/user/updateById.do", this.formData).then((res) => {
                    if (res.data.flag) {
                        this.$message({
                            message: res.data.message,
                            type: "success"
                        });
                    } else {
                        this.$message.error(res.data.message);
                    }
                }).finally(() => {
                    this.findHead();
                });
            },
            //修改密码显示框
            ChangePassword(row) {
                this.updateByPassword = true;
                this.formData = {};
                axios.get("/user/findBy.do?id=" + row.id).then((res) => {
                    this.formData = res.data.data;
                })
            },
            //修改密码
            submitForm(formData) {
                //密码验证
                /* var cipher = this.formData.cipher;
                 alert(cipher);
                 var cryptogram =this.formData.cryptogram;
                 alert(cryptogram);
                 if (!usernamePassword(cipher)){
                     this.$message.error('请输入6-16位的密码');
                     return;
                 }*/
                this.updateByPassword = false;
                axios.post("/user/updatePassword.do",
                    {
                        "cipher": this.formData.cipher,
                        "cryptogram": this.formData.cryptogram,
                        "id": this.formData.id
                    }).then((res) => {
                    if (res.data.flag) {
                        this.$message({
                            message: res.data.message,
                            type: "success"
                        });
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            //删除表
            handleDelete(row) {
                //删除前进行提示
                this.$confirm("确认删除吗？", "提示", {type: 'warning'}).then(() => {
                    //发送异步请求
                    axios.get("/user/delete.do?id=" + row.id).then((response) => {
                        if (response.data.flag) {
                            this.findPage();
                            //删除成功
                            this.$message({
                                message: response.data.message,
                                type: 'success'
                            });
                        } else {
                            //删除失败
                            this.$message(response.data.message)
                        }
                    }).finally(() => {
                        this.findHead();
                    });
                });
            },
            //角色授权回显
            AddingRoles(row) {
                this.dialogFormVisibleUser = true;
                this.activeName = 'first';
                axios.get("/user/AddingRoles.do").then((res) => {
                    if (res.data.flag) {
                        //双向绑定
                        this.tableData = res.data.data;
                        //回显复选框
                        axios.get("/user/findByUserAndRole.do?id=" + row.id).then((res) => {
                            if (res.data.flag) {
                                this.RoleIds = res.data.data;
                                //获取id值
                                axios.get("/user/findById.do?id=" + row.id).then((res) => {
                                    if (res.data.flag) {
                                        this.formData = res.data.data;
                                    }
                                });
                            } else {
                                this.$message.error(res.data.message);
                            }
                        });
                    }
                })
            },
            //角色授权确认
            RoleAuthorization() {
                this.dialogFormVisibleUser = false;
                axios.post("/user/RoleAuthorization.do?role=" + this.RoleIds, this.formData).then((res) => {
                    if (res.data.flag) {
                        this.$message({
                            message: res.data.message,
                            type:'success'

                        });
                    } else {
                        this.$message.error(res.data.message);
                    }
                });

            }
        }
    })
</script>
</html>